<?php
/*首页数据接口*/
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index() {
        $this->display();
    }
    public function getDataJson() {
        $data['com.jindouche.tiyan']['1.1.0']=true;
        $data['com.jindouche.admin']['1.0.0']=true;
        $data['com.jindouche.admin']['1.1.0']=true;
        $this -> ajaxReturn($data,'JSONP');
    }
    public function getIndexData() {
        //实例化IndexModel;
        $indexModel = D("Index");
        $city=I('request.city');
        //车辆数据
        $carData = $indexModel->getCarData($city);
        $this -> ajaxReturn($carData,'JSONP');
    }
    /*
        功能:获取新车数据
    */
    public function getNewCarData() {
        // //拼接查询条件
        $where = 'jdc_cars.is_show=1 and jdc_price.purchase_scheme=1 and jdc_price.is_new=1';

        //指导价范围
        if(I('request.car_price')){
           switch (I('request.car_price')) {          
                /*case '0':
                    //全部
                    $order = "first_pay asc";
                    break;*/
                case '1':
                    //2~3万
                    $where.=' and jdc_cars.guide_price >= 50000 and jdc_cars.guide_price < 100000';
                    break;
                case '2':
                    //3~4万
                    $where.=' and jdc_cars.guide_price >= 100000 and jdc_cars.guide_price < 150000';
                    break;
                case '3':
                    //4~5万
                    $where.=' and jdc_cars.guide_price >= 150000 and jdc_cars.guide_price < 200000';
                    break;
                case '4':
                    //5万以上
                    $where.=' and jdc_cars.guide_price >= 200000  and jdc_cars.guide_price < 250000';
                    break; 
                case '5':
                    //5万以上
                    $where.=' and jdc_cars.guide_price >= 250000 and jdc_cars.guide_price < 300000';
                    break;                                              
                default:
                    # code...
                    break;
            } 
        }
        //车型
        $cartype=null;
         if (I('request.cartype')) {
            $cartype = I('request.cartype');
            $where.=" and jdc_cars.type like '%$cartype%'";
        }
       /* //首次进入
        if(empty(I('request'))) {
            $order ="add_time desc";
        }*/
        //默认排序
        if(I('request.sort')){
          switch (I('request.sort')) {
            /*case '0':
                //默认排序
                $order ="add_time desc";
                break;*/
            case '1':
                //月供最低排序
                $order ="jdc_price.month_pay asc"; 
                break;
            case '2':
                //月供最高排序
                $order ="jdc_price.month_pay desc";
                break;
             default:
                # code...
                break;
            }  
        }   
        //首付范围
        if(I('request.month_pay')){
           switch (I('request.month_pay')) {          
                /*case '0':
                    //全部
                    $order = "first_pay asc";
                    break;*/
                case '1':
                    //2~3万
                    $where.=' and jdc_price.month_pay >= 2000 and jdc_price.month_pay < 3000';
                    break;
                case '2':
                    //3~4万
                    $where.=' and jdc_price.month_pay >= 3000 and jdc_price.month_pay < 4000';
                    break;
                case '3':
                    //4~5万
                    $where.=' and jdc_price.month_pay >= 4000 and jdc_price.month_pay < 5000';
                    break;
                case '4':
                    //5万以上
                    $where.=' and jdc_price.month_pay >= 5000  and jdc_price.month_pay < 10000';
                    break; 
                case '5':
                    //5万以上
                    $where.=' and jdc_price.month_pay >= 10000 ';
                    break;                                              
                default:
                    # code...
                    break;
            } 
        }

        //获取城市名称
        $city=null;
        if (I('request.city')) {
            $city = I('request.city');
            $cityObj = M('city');
            $where_map = "name like'%{$city}%' and is_show=1";
            $cityInfo = $cityObj->where($where_map)->field('id')->find(); 
            $where.=' and jdc_price.city_id = '.$cityInfo['id'];
        }
        //获取品牌名称
        $brand=null;
         if (I('request.brand')) {
            $brand = I('request.brand');
            $where.=" and jdc_cars.brand like '%$brand%'";
        }

        //获取品牌名称
        $name=null;
         if (I('request.name')) {
            $name = I('request.name');
            $where.=" and jdc_cars.name like '%$name%'";
        }
        //获取要查询的页数
        $page=1;
        if(I('request.page')) {
            $page=I('request.page');
        }
        // //实例化IndexModel;
        $indexModel = D("Index");
        // //按查询条件获取新车数据
        $newCarData = $indexModel->getNewData($where, $order, $page);
        //$newCarData = I('request.city');
        $this->ajaxReturn($newCarData,'JSONP');
    }
    public function getDetailDate(){
        //获取城市
        $city=M('city');
        $where="name='".I('request.city')."' and is_show=1";
        $field="id";
        $city_info=$city->field($field)->where($where)->find();
        $fileName="CarJson/".I('request.carnum').'-'.$city_info['id'].'.json';
        $result=ossGetObject($fileName);
        if($result){
           $data=json_decode($result); 
        }else{
            
            //车辆数据
            $detail = D("Index");
            $detailData = $detail->getDetailsDate(I('request.carnum'));
            $carsData = $detail->getCarsDate(I('request.carnum'));
            $carPrice = $detail->getCarPrice(I('request.carnum'),$city_info['id']);
            $carRecommend = $detail->getCarsRecommend($carsData,$city_info['id']);
            $data=array('carData'=>array_merge($detailData,$carsData),'carPrice'=>$carPrice,'carRecommend'=>$carRecommend);
            ossPutObject($fileName,json_encode($data));
        }
        
        $this->ajaxReturn($data,'JSONP');
    }
    public function getUserOrder(){
        $indexModel = D("Index");
        $data = $indexModel->getUserOrder(I('request.mobilephone'));
        $this->ajaxReturn($data,'JSONP');
    }
    public function getCarList(){
        $indexModel = D("Index");
        $data = $indexModel->getCarList();
        $this->ajaxReturn($data,'JSONP');
    }
    public function setUserOrder(){
        
        $indexModel = D("Index");
        $order_db = M('order');
        $result = getDocumentStatus(I('request.orderid'));
        if (in_array("error", $result) && $result['status'] == 'error') {
            $this->ajaxReturn(['code' => 0],'JSONP');
        } elseif (in_array('signing', $result) && $result['documentStatus'] == 'signing') {
            $order_res = $order_db -> where('orderid='.I('request.orderid')) -> find();
            $this->ajaxReturn(['code' => 1, 'url' => $order_res['lingQianUrl']],'JSONP');
        } elseif (in_array('signed', $result) && $result['documentStatus'] == 'signed') {
            $indexModel->setUserOrder(I('request.orderid'));
            $this->ajaxReturn(['code' => 2],'JSONP');
        }
    }

    public function getOrderPay(){
        
        $indexModel = D("Index");
        $data = $indexModel->getOrderPay(I('request.mobilephone'));
        $this->ajaxReturn($data,'JSONP');
    }

    public function checkEvaluate(){
        
        $evaluate = M("evaluate");
        $data = $evaluate->where(array('orderid'=>I('request.orderid'),'type'=>I('request.type')))->find();
        if($data){
            $result=1;
        }else{
            $result=0;
        }
        $this->ajaxReturn($result,'JSONP');
    }
    
    public function uploadEvaluate(){
        $evaluate = M("evaluate");
        $data["orderid"]=I('request.orderid');
        $data["type"]=I('request.type');
        $data["score"]=I('request.score');
        $data["desc"]=I('request.desc');
        $data["create_time"]=time();
        $result = $evaluate->add($data);
        $this->ajaxReturn($result,'JSONP');
    }
    public function addCollect(){
        $user = M("user");
        $where["tel"]=I('request.mobilephone');

        $result = $user->where($where)->find();
        if($result['collect']){
            $data['collect']=$result['collect'].','.I('request.carnum');
        }else{
            $data['collect']=I('request.carnum');
        }
        $user->where($where)->save($data);
        $this->ajaxReturn($data['collect'],'JSONP');
    }
    public function delCollect(){
        $user = M("user");
        $where["tel"]=I('request.mobilephone');

        $result = $user->where($where)->find();
        $collect_arr=explode(',',$result['collect']);
        $key=array_search(I('request.carnum'),$collect_arr);
        if($collect_arr[$key]){
            unset($collect_arr[$key]);
        }
        if(count($collect_arr)>0){
             $data['collect']=implode(',',$collect_arr);
        }else{
            $data['collect']="";
        }
        $user->where($where)->save($data);
        $this->ajaxReturn($data['collect'],'JSONP');
    }
    public function getCollect(){
        $user = M("user");
        $where["tel"]=I('request.mobilephone');

        $result = $user->where($where)->find($data);
        $collect_arr=explode(',',$result['collect']);
        $car = M('cars');
        $field = "*";
        $where_car['carnum']=array("in",$collect_arr);
        $where_car['is_show']=1;
        $new_car = $car->field($field)->where($where_car)->select();


        //获取城市
        $city=M('city');
        $where="name='".I('request.city')."' and is_show=1";
        $field="id";
        $city_info=$city->field($field)->where($where)->find();
        //获取价格
        $price=M('price');
        $field="carnum,month_pay";
        $where_price['carnum']=array("in",$collect_arr);
        $where_price['city_id']=$city_info['id'];
        $price_infos=$price->field($field)->where($where_price)->select();
        foreach($price_infos as $val){
            $price_data[$val['carnum']]=$val['month_pay'];
        }

        foreach($new_car as $key=>$val){
            $new_car[$key]['month_pay']=$price_data[$val['carnum']];
        }
        $this->ajaxReturn($new_car,'JSONP');
    }

    public function addDevice()
    {
        $device_db = M('device');
        $device_res = $device_db -> where('uuid='.I('request.uuid')) -> find();
        if (!$device_res) {
            $data = [
                'uuid' => I('request.uuid')
            ];
            $id = $device_db -> add($data);
            $this -> ajaxReturn(['uuid' => I('request.uuid'), 'error' => 1], 'JSONP');
        }
    }

    public function getBaike()
    {
        $car_baike = M('car_baike');
        $baike_res = $car_baike->field('id,title') -> where('is_show=1') -> select();
        $this -> ajaxReturn($baike_res, 'JSONP');
    }

    public function getBaikeDetail()
    {
        $car_baike = M('car_baike');
        $baike_res = $car_baike->field('id,title,content') -> where('id='.I('request.id')) -> find();
        $this -> ajaxReturn($baike_res, 'JSONP');
    }
}
